#!/bin/bash

### 设置参数
mysql_host=127.0.0.1
mysql_passwd=123456
mysql_port=3306

my_email=jlcon@163.com
my_email_passwd=jlcon123

rabbitMQ_addr=127.0.0.1:5672
rabbitMQ_user_info=admin:123456
host_addr=`ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"`

### 执行命令
cd /opt
git clone https://gitee.com/free_cap/HttpRunnerManager.git
sleep 1s
# requirements配置
sed -ri "s/mysqlclient == 1.3.12/mysqlclient/" /opt/HttpRunnerManager/requirements.txt
sed -ri "s/Django == 2.0.3/Django == 2.1.2/" /opt/HttpRunnerManager/requirements.txt
echo -e "\npymysql" >> /opt/HttpRunnerManager/requirements.txt

pip install -r /opt/HttpRunnerManager/requirements.txt

mysql -u root --password="${mysql_passwd}" -e "create database HttpRunner;"

# mysql配置
sed -ri "s/Hst888888/$mysql_passwd/" /opt/HttpRunnerManager/HttpRunnerManager/settings.py
sed -ri "s/'HOST': '192.168.91.45'/'HOST': '$mysql_host'/" /opt/HttpRunnerManager/HttpRunnerManager/settings.py
sed -ri "s/'PORT': '3306'/'PORT': '$mysql_port'/" /opt/HttpRunnerManager/HttpRunnerManager/settings.py

# 邮箱配置
sed -ri "s/quanwang.yin@hstong.com/$my_email/" /opt/HttpRunnerManager/HttpRunnerManager/settings.py
sed -ri "s/TANGxinbing135!/$my_email_passwd/" /opt/HttpRunnerManager/HttpRunnerManager/settings.py

# rabbitmq配置
sed -ri "s/BROKER_URL = 'amqp:\/\/dev:zwc123@192.168.91.45:5672\/\/' if DEBUG else 'amqp:\/\/dev:zwc123@192.168.91.45:5672\/\/'/BROKER_URL = 'amqp:\/\/$rabbitMQ_user_info@$rabbitMQ_addr\/\/'/" /opt/HttpRunnerManager/HttpRunnerManager/settings.py


cd /opt/HttpRunnerManager
python manage.py makemigrations ApiManager
python manage.py migrate
python manage.py createsuperuser
sleep 1s
echo '添加防火墙'
firewall-cmd --permanent --zone=public --add-port=8000/tcp
firewall-cmd --reload

echo "=============================信息============================="
echo "后台管理地址：http://$host_addr:8000/admin"
echo "前台地址：http://$host_addr:8000/api/login"


# 启动服务
nohup python manage.py runserver 0.0.0.0:8000 >> ./httprunner.log 2>&1 &